package practice11;
import practice11.entity.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class StudentMapperTest {
    @Test
    public void StudentTest() throws IOException {
        // 构建SqlSessionFactory并打开一个自动提交的SqlSession
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml")).openSession(true);
        StudentMapper deptMapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = deptMapper.selectStudent("1","1");
        System.out.println(studentList);



    }
    //插入多条学生数据
    @Test
    public void StudentTest01() throws IOException {
        // 构建SqlSessionFactory并打开一个自动提交的SqlSession
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml")).openSession(true);
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

        ArrayList<Student> list = new ArrayList<>();
        for (int i = 0; i < 10; i++){
            list.add(new Student(10+i,1,"ynny"+i,20+i,"女"));

        }
        int i = studentMapper.insertStudent(list);
        System.out.println(i);


    }

    //根据学号修改学生记录
    @Test
    public void StudentTest02() throws IOException {
        // 构建SqlSessionFactory并打开一个自动提交的SqlSession
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml")).openSession(true);
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setStudentId(1);
        student.setAge(20);
        student.setClassId(1);
        student.setGender("女");
        student.setName("ynny");
        int i = studentMapper.updateStudent(student);
        System.out.println(i);


    }
    //根据学号删除学生记录
    @Test
    public void StudentTest03() throws IOException {
        // 构建SqlSessionFactory并打开一个自动提交的SqlSession
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml")).openSession(true);
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        int i = studentMapper.deleteStudent("1");
        System.out.println(i);

    }
}
